package me.acomma.groot.interfaces.controller;

import me.acomma.groot.application.menu.MenuApplicationService;
import me.acomma.groot.application.menu.MenuData;
import me.acomma.groot.application.role.*;
import me.acomma.groot.infrastructure.Pagination;
import me.acomma.groot.infrastructure.RestResult;
import me.acomma.groot.application.role.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/role")
public class RoleController {
    @Autowired
    private RoleApplicationService roleApplicationService;

    @Autowired
    private MenuApplicationService menuApplicationService;

    @GetMapping("/page")
    public RestResult page(PageQueryRoleCommand command) {
        Pagination<RoleData> pagination = roleApplicationService.pageQuery(command);
        return RestResult.success(pagination);
    }

    @GetMapping("/detail")
    public RestResult detail(@RequestParam("roleId") Long roleId) {
        RoleData data = roleApplicationService.get(roleId);
        return RestResult.success(data);
    }

    @PostMapping("/create")
    public RestResult create(@RequestBody CreateRoleCommand command) {
        roleApplicationService.create(command);
        return RestResult.success();
    }

    @PostMapping("/modify")
    public RestResult modify(@RequestBody ModifyRoleCommand command) {
        roleApplicationService.modify(command);
        return RestResult.success();
    }

    @PostMapping("/remove")
    public RestResult remove(@RequestBody RemoveRoleCommand command) {
        roleApplicationService.remove(command);
        return RestResult.success();
    }

    @GetMapping("/all")
    public RestResult all() {
        List<RoleData> roles = roleApplicationService.getAll();
        return RestResult.success(roles);
    }

    @GetMapping("/menu")
    public RestResult assignedMenu(@RequestParam("roleId") Long roleId) {
        List<MenuData> tree = menuApplicationService.getMenuByRoleId(roleId);
        return RestResult.success(tree);
    }

    @PostMapping("/menu")
    public RestResult assignMenu(@RequestBody AssignMenuCommand command) {
        roleApplicationService.assignMenu(command);
        return RestResult.success();
    }
}
